package com.chuyu.cy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuyu.cy.entity.domain.FeedBack;
import com.chuyu.cy.entity.domain.User;
import com.chuyu.cy.entity.dto.FeedBackDTO;
import com.chuyu.cy.exception.CustomException;
import com.chuyu.cy.mapper.FeedBackMapper;
import com.chuyu.cy.mapper.UserMapper;
import com.chuyu.cy.service.FeedBackService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author white-zhou
* @description 针对表【feed_back(反馈表)】的数据库操作Service实现
*/
@Service
public class FeedBackServiceImpl extends ServiceImpl<FeedBackMapper, FeedBack>
    implements FeedBackService{

    @Resource
    private FeedBackMapper feedBackMapper;
    @Resource
    private UserMapper userMapper;


    /**
     * 添加反馈
     * @param feedBackDTO 反馈信息
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addFeedBack(FeedBackDTO feedBackDTO) {
        if (feedBackDTO == null) {
            throw new CustomException("参数不能为空");
        }
        if (feedBackDTO.getUserId() == null || feedBackDTO.getFeedBackId() == null) {
            throw new CustomException("用户ID或反馈者ID不能为空");
        }

        User user = userMapper.selectById(feedBackDTO.getFeedBackId());
        if (user == null) {
            throw new CustomException("反馈者不存在");
        }

        FeedBack feedBack = new FeedBack();
        BeanUtil.copyProperties(feedBackDTO, feedBack);

        int insert = feedBackMapper.insert(feedBack);
        if (insert <= 0) {
            throw new CustomException("添加反馈失败");
        }

        return true;
    }
}




